package com.tbvtc.agriculturalcreditchainserver2025.utils.Pest;

import com.tbvtc.agriculturalcreditchainserver2025.pojo.pear.MainPestPear;
import com.tbvtc.agriculturalcreditchainserver2025.utils.CustomMultipartFile;
import com.tbvtc.agriculturalcreditchainserver2025.utils.http.HttpConstant;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

@Component
public class PestHttpUtils {

    private final RestTemplate restTemplate;

    public PestHttpUtils(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 获取图像识别结果
     *
     * @param imageBytes
     * @param filename
     * @return
     */

    public String uploadImage(byte[] imageBytes, String filename) {
        // 1. 将字节数组包装为 Resource，并保留文件名
        ByteArrayResource resource = new ByteArrayResource(imageBytes) {
            @Override
            public String getFilename() {
                return filename; // 服务端可能需要文件名
            }
        }; // 获取字节数组
        MultipartFile multipartFile = new CustomMultipartFile(
                imageBytes,
                "file",
                "image.jpg",
                "image/jpeg"
        );

        // 2. 构建 multipart/form-data 请求体
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", multipartFile.getResource());
        // 3. 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 4. 创建请求实体
        HttpEntity<MultiValueMap<String, Object>> requestEntity =
                new HttpEntity<>(body, headers);

        // 5. 发送 POST 请求
        String url = HttpConstant.predictURL_ADDRESS;
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                String.class
        );

        return response.getBody();
    }

    /**
     * 获取摄像头图像
     *
     * @param quality
     * @param width
     * @return
     */
    public String getCaptureImage(Integer quality, Integer width) {
        MultiValueMap<String, Integer> params = new LinkedMultiValueMap<>();
        params.add("quality", quality);
        params.add("width", width);
        // 发送POST请求
        return restTemplate.postForObject(
                HttpConstant.captureURL_ADDRESS,
                params,
                String.class
        );
    }

    public String getPearCaptureImage() {
        // 发送POST请求
        return restTemplate.postForObject(HttpConstant.pearURL_ADDRESS, "", String.class);
    }

    //  //传入ai农作物，农药，用了几次，是否合格
    public String getTreatmentMethodByAi(String cropName, String pesticideName, String numbers, Long times, String isQualified) {
        String prompt = cropName + "使用了" + pesticideName + "用量为" + numbers + "经过检查" + isQualified + "本季度剩余用量最好是" + times + "次，请给出建议";
        String url = HttpConstant.pestChatURL_ADDRESS + prompt;
        // 发送 GET 请求并接收响应
        return restTemplate.getForObject(url, String.class);


    }

    public String uploadPearImage(byte[] imageBytes, String filename) {
        // 1. 将字节数组包装为 Resource，并保留文件名
        ByteArrayResource resource = new ByteArrayResource(imageBytes) {
            @Override
            public String getFilename() {
                return filename; // 服务端可能需要文件名
            }
        }; // 获取字节数组
        MultipartFile multipartFile = new CustomMultipartFile(
                imageBytes,
                "file",
                "image.jpg",
                "image/jpeg"
        );

        // 2. 构建 multipart/form-data 请求体
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", multipartFile.getResource());
        // 3. 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 4. 创建请求实体
        HttpEntity<MultiValueMap<String, Object>> requestEntity =
                new HttpEntity<>(body, headers);

        // 5. 发送 POST 请求
        String url = HttpConstant.pearPestURL_ADDRESS;
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                String.class
        );
        return response.getBody();
    }


}